
import {BookModel} from "../../models/book"
import {LikeModel} from "../../models/like"
const bookModel=new BookModel()
const likeModel=new LikeModel()


Page({
    data: {
        comments:[],
        book:null,
        likeStatus:false,
        likeCount:0,
        posting:false

    },
    onLoad: function (options) {
        wx.showLoading()
        const bid = options.bid

        const detail = bookModel.getDetail(bid)
        const comments = bookModel.getComment(bid)
        const likeStatus = bookModel.getLikeStatus(bid)
        //优化代码 v1
        Promise.all([detail,comments,likeStatus]).then(res=>{
            console.log(res)
            this.setData({
                book: res[0],
                comments: res[1].comments,
                likeStatus: res[2].like_status,
                likeCount: res[2].fav_nums
            })
            wx.hideLoading()
        })





    }

    ,
    onLike:function (event)
    {
        let behavior = event.detail.behavior
        likeModel.like(behavior, this.data.book.id,400)
    },


    //输入短评页面
    onFakePost:function(event){
        this.setData({
            posting:true
        })
        console.log(this.data.posting)
    },
    //关闭短评页面
    onCancel:function(event){
        this.setData({
            posting:false
        })
    },


    //点击提交短评方法
    onPost(event){
        const comment = event.detail.text || event.detail.value

        if(!comment){
            return
        }

        if (comment.length > 12) {
            wx.showToast({
                title: '短片最多12个字',
                icon: 'none'
            })
            return
        }

        bookModel.postComment(this.data.book.id, comment).then(res => {
            wx.showToast({
                title: '+1',
                icon: 'none'
            })

            this.data.comments.unshift({
                content: comment,
                nums: 1
            })
            //更新短评
            this.setData({
                comments: this.data.comments,
                //点击完成页面关闭
                posting:false
            })
        })
    },





})
